package net.m510.autosurfer.idemitsu;

import java.net.URI;
import java.util.logging.Logger;

import net.m510.autosurfer.common.DocumentGetProcess;
import net.m510.autosurfer.common.exception.InvalidDocumentException;

import org.apache.http.HttpHost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class IdemitsuGoSpendingDetail extends DocumentGetProcess {
	public IdemitsuGoSpendingDetail() {
		this(null,null);
	}

	public IdemitsuGoSpendingDetail(CloseableHttpClient client, HttpClientContext context) {
		super(client, context);
		super.log = Logger.getLogger(IdemitsuGoSpendingDetail.class.getName());
	}

	@Override
	protected URI getUri(Document doc) {
		Elements e = doc.getElementsByAttributeValueContaining("alt", "ご利用明細照会");
		if ( e.size() == 0 ) throw new InvalidDocumentException("ご利用明細照会への遷移タグが見つかりません", doc);
		log.fine("image src="+e.first().outerHtml());
		log.fine("parent src="+e.first().parent().outerHtml());
		String anchor = e.first().parent().attr("href");
		if ( anchor == null ) throw new InvalidDocumentException("ご利用明細照会へのURLが見つかりません", doc);
		HttpHost host = context.getTargetHost();
		URI uri = URI.create(host.toURI());
		uri = uri.resolve(anchor);
		return uri;
	}
}
